websocketemit('getorderlist', null);

onwebsocket('getorderlist', (data) => {
    var itemTemplate = "<div style='background:#bddeff;margin:10px;'>\
    订单编号：<span>{0}</span>\
    截止日期：<span>{1}</span>\
    订单描述:<span>{2}</span>\
    状态：<span>{4}</span>\
    {5}\
    <div style='background:#d6e7f8'><ul>{3}</ul></div>\
    </div>";
    var htmls = "";
    var i = 0;

    for(var key in data.results){
        var item = JSON.parse(data.results[key]);
        //文件信息
        var infoHtml = "";
        var fileinfos = data.infos[i];
        var infoTemplate = "<li>编号：{0} 文件名:{1}</li>";        
        for(var f = 0; f < fileinfos.length; f++){      
            infoHtml += infoTemplate.format(item.ids[f], fileinfos[f].name);
        }

        if(infoHtml == ""){
            infoHtml = "无";
        }

        var stateStr = GetStateText(item.state);

        var stateBtn = "";
        if(item.state == 0){
            stateBtn = "<button onclick='BtnDeleteOrderClick(\"" + item.orderid + "\")'>删除</button>";
        }

        htmls += itemTemplate.format(item.orderid, item.endtime, item.desc, infoHtml, stateStr, stateBtn); 
        
        i++;   
    }
    document.getElementById("resourceList").innerHTML = htmls;
});

function GetStateText(state){
    if(state == 1){
        return "已完成";
    }
    return "进行中";
}

function BtnDeleteOrderClick(orderid){
    websocketemit('removeorderitem', {orderid:orderid});
}

onwebsocket('removeorderitem', (data)=>{
    if(data == 1){
        websocketemit('getorderlist', null);
    }
});